package com.ittx.dzt.meituan.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.OrderGetDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.meituan.constant.MeituanApiConstant;
import com.ittx.dzt.meituan.handler.AbstractBaseMeituanApiHandler;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据订单号下载订单详情
 * <p>
 * cy.salesOrder.download.byOrderId
 * /api/v1/gw/order/detail(查询订单详情信息)  https://tscc.meituan.com/home/doc/bulkPurchasing/512
 * </p>
 *
 * @author gywnag
 * @since 2024-08-01
 */
@Slf4j
@Service
public class MeituanSalesOrderDownloadByIdService extends AbstractBaseMeituanApiHandler<OrderGetDTO> {


    @Override
    public String getMeituanMethod() {
        return MeituanApiConstant.ORDER_DETAIL;
    }

    @Override
    public String getHttpMethod() {
        return MeituanApiConstant.ApiHttpMethod.POST;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.SALESORDER_DOWNLOAD_BY_ORDERID;
    }

    @Override
    public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, OrderGetDTO orderGetDTO) {
        // 参数校验
        Result<?> result = checkParams(platformApiParamDTO, orderGetDTO);
        if (result.hasError()) {
            return result;
        }
        List<Object> list = new ArrayList<>();
        String[] orderCodeArr = orderGetDTO.getOrderId().split(",");
        if (orderCodeArr.length > 0) {
            for (String code : orderCodeArr) {
                result = this.getOrderById(platformApiParamDTO, code);
                if (result.hasError()) {
                    return result;
                }
                Object resultData = result.getData();
                // 同步返回
                if (orderGetDTO.getIsAsync() == 0) {
                    list.add(resultData);
                } else {
                    platformApiParamDTO.setPlatformIdentifierTag(code);
                    result = this.sendRequestToPlatform(platformApiParamDTO, resultData, true);
                    if (!result.isSuccess()) {
                        return result;
                    }
                }
            }
        }
        // 同步返回
        if (orderGetDTO.getIsAsync() == 0) {
            return Result.success().addData(list).addMsg(platformApiParamDTO.getStoreConfig().getChainUniqueCode());
        }
        return Result.success();
    }


    /**
     * 根据id获取订单
     *
     * @param platformApiParamDTO
     * @param id
     * @return
     */
    public Result<?> getOrderById(PlatformApiParamDTO platformApiParamDTO, String id) {
        return this.getOrderBySdk(platformApiParamDTO, id);
    }


    /**
     * 执行api请求
     *
     * @param platformApiParamDTO
     * @param id
     * @return 请求结果 resultMap.get(DATA_FIELD)
     */
    public Result<?> getOrderBySdk(PlatformApiParamDTO platformApiParamDTO, String id) {
        Map<String, String> params = new HashMap<>();
        params.put("order_view_id", id);
        params.put("poi_id", getPoIId(platformApiParamDTO));
        StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
        String requestId = platformApiParamDTO.getRequestId();
        Result<?> result = this.executeMeituanApi(storeConfig, params, requestId);
        if (!result.isSuccess()) {
            return result;
        }
        Map<String, Object> resultMap = (Map<String, Object>) result.getData();
        Map<String, Object> saleData = (Map<String, Object>) resultMap.get(DATA_FIELD);
        List<Map<String, Object>> sale = (List<Map<String, Object>>) saleData.get("order_list");
        if (CollUtil.isEmpty(sale)) {
            String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0009_MSG, requestId, id);
            log.error(msg);
            return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0009, msg));
        }
        return Result.success().addData(sale.get(0));
    }

    /**
     * 参数校验
     *
     * @param platformApiParamDTO 平台请求参数
     * @param orderGetDTO         接口请求参数
     */
    public Result<?> checkParams(PlatformApiParamDTO platformApiParamDTO, OrderGetDTO orderGetDTO) {
        if (orderGetDTO == null || platformApiParamDTO.getStoreConfig() == null || StrUtil.isBlank(orderGetDTO.getOrderId())) {
            String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0004_MSG, platformApiParamDTO.getRequestId(),
                    this.getPlatformCode(), getLocalApiMethod(), orderGetDTO);
            log.error(msg);
            return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0004, msg));
        }
        return Result.success();
    }
}
